#!/bin/sh

BUILD_VERSION="v`date +%Y%m%d%H%M`"
REGISTRY_SERVER=harbor.zhixueyun.com:5000/zxy9
PRIVATE_TOKEN="5hqRTSXBJ9cQMKPzhouyuyxz"
TAG_NAME="${PROJECT_VERSION}-v`date +%Y%m%d`"
TAG_API_URL="https://gitlab.zhouyu.com/api/v3/projects/zxy%2f$PROJECT_NAME/repository/tags"

for NAME in "service" "web-server" "async-service"; do
    # 第一步:构建镜像
    if [ -e "${PROJECT_NAME}-${NAME}" ]; then
        cd "${PROJECT_NAME}-${NAME}"
        mkdir tmp
        cp Dockerfile tmp
        cp "target/${PROJECT_NAME}-${NAME}-${PROJECT_VERSION}.jar" tmp
        cd tmp
        docker login -u common -p Harbor12345 harbor.zhixueyun.com:5000
        docker build -t "${REGISTRY_SERVER}/${PROJECT_NAME}-${NAME}:${BUILD_VERSION}-${VERSION}" .
        docker push "${REGISTRY_SERVER}/${PROJECT_NAME}-${NAME}:${BUILD_VERSION}-${VERSION}"
        docker rmi "${REGISTRY_SERVER}/${PROJECT_NAME}-${NAME}:${BUILD_VERSION}-${VERSION}"
        cd ..
        rm -rf tmp
        cd ..
    fi
## 第二步:推包
    if [ $POST_ADDRESS = dev9 ]
        then
            curl -XPUT -d password=passit 192.168.110:8005/$PROJECT_NAME/$NAME/$BUILD_VERSION-$VERSION
        elif [ $POST_ADDRESS = zxy9 ]
        then
            curl -XPUT -d password=passit 192.168.110:8005/$PROJECT_NAME/$NAME/$BUILD_VERSION-$VERSION
        elif [ $POST_ADDRESS = prezxy9 ]
        then
            curl -XPUT -d password=passit 192.168.110:8005/$PROJECT_NAME/$NAME/$BUILD_VERSION-$VERSION
        elif [ $POST_ADDRESS = prodev9 ]
        then
            curl -XPUT -d password=passit 192.168.110:8005/$PROJECT_NAME/$NAME/$BUILD_VERSION-$VERSION
        elif [ $POST_ADDRESS = chinamobile ]
        then
            curl -XPUT -d password=passit 192.168.110:8006/$PROJECT_NAME/$NAME/$BUILD_VERSION-$VERSION
            curl --request DELETE --header "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" "${TAG_API_URL}/${TAG_NAME}"
            curl --request POST --header "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" "${TAG_API_URL}?tag_name=${TAG_NAME}&ref=${VERSION}"
        elif [ $POST_ADDRESS = performance ]
        then
            curl -XPUT -d password=passit 192.168.110:8005/$PROJECT_NAME/$NAME/$BUILD_VERSION-$VERSION
        elif [ $POST_ADDRESS = empty ]
        then
          echo "No push address" 
    fi
done

